﻿''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>

Public Class Camera
    Inherits Microsoft.Xna.Framework.GameComponent

    Public _GameManager As GameManagerCollection

    Public AspectRatio As Single = MyBase.Game.GraphicsDevice.Viewport.AspectRatio ' The aspectratio of the model
    Public ViewMatrix As Matrix ' The postion and view direction of the camera
    Public ProjectionMatrix As Matrix ' The matrix that determines how the world is seen.
    Public WorldMatrix As Matrix = Matrix.Identity ' The rotation matrix, set as a unit matrix
    Public DefaultCameraPosition As Vector3 = New Vector3(0, 0, 0) ' The world space position of the model
    Public HorizontalRotation As Single = MathHelper.PiOver2 / 10 ' The rotation angle in degrees for the x direction
    Public VerticalRotation As Single = -MathHelper.Pi / 8.0F ' The rotation angle in degrees for the y direction
    Public RotationSpeed As Single = 0.3F
    Public MoveSpeed As Single = 60.0F
    Private m_position As Vector2
    Public ReadOnly Property MousePosition() As Vector2
        Get
            Return m_position
        End Get
    End Property

    Public Sub New(ByVal Game As DanoQuest)
        MyBase.New(Game)
        Me.Game.Components.Add(Me)
        _GameManager = Game.GameManager
        m_position = New Vector2(CInt(Game.Resolution.X / 2), CInt(Game.Resolution.Y / 2))
        ViewMatrix = Matrix.CreateLookAt(DefaultCameraPosition, New Vector3(0, 0, 0), New Vector3(0, 1, 0))
        ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, AspectRatio, 0.2F, 2000.0F)
    End Sub

    ''' <summary>
    ''' Allows the game component to perform any initialization it needs to before starting
    ''' to run.  This is where it can query for any required services and load content.
    ''' </summary>
    Public Overrides Sub Initialize()
        ' TODO: Add your initialization code here
        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' Allows the game component to update itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Public Overrides Sub Update(ByVal gameTime As GameTime)
        ' TODO: Add your update code here
        UpdateViewMatrix()
        MyBase.Update(gameTime)
    End Sub

    Public Sub UpdateViewMatrix()
        ' rotationx += 1f; Matrix.CreateRotationX(MathHelper.ToRadians(RotationX)) *  Matrix.CreateRotationY(MathHelper.ToRadians(RotationY))
        Dim cameraRotation As Matrix = Matrix.CreateRotationX(VerticalRotation) * Matrix.CreateRotationY(HorizontalRotation)
        Dim cameraRotatedTarget As Vector3 = Vector3.Transform(Vector3.Forward, cameraRotation)
        Dim cameraFinalTarget As Vector3 = Game.Components.OfType(Of Player).ToList.Item(0).Location + cameraRotatedTarget
        Dim cameraRotatedUpVector As Vector3 = Vector3.Transform(Vector3.Up, cameraRotation)
        ViewMatrix = Matrix.CreateLookAt(Game.Components.OfType(Of Player).ToList.Item(0).Location, cameraFinalTarget, cameraRotatedUpVector)
    End Sub

End Class
